package com.alen.sun.controller;

import com.alen.sun.bean.CheckIn;
import com.alen.sun.bean.Elderly;
import com.alen.sun.bean.PageBean;
import com.alen.sun.bean.Result;
import com.alen.sun.dao.ElderlyMapper;
import com.alen.sun.service.ActivityService;
import com.alen.sun.service.CheckInService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RequestMapping("/checkIns")
@RestController
public class CheckInController {
    @Autowired
    private CheckInService checkInService;
    @Autowired
    private ElderlyMapper elderlyMapper;

    /**
     * 添加
     * @param checkIn
     * @return
     */
    @PostMapping
    @ResponseBody
    public Result addCheckIn(@RequestBody CheckIn checkIn){
        Boolean flag = checkInService.addCheckIn(checkIn);
        if(flag) {
            return Result.success();
        }else {
            return Result.error("添加失败");
        }
    }

    /**
     * 修改
     * @param checkIn
     * @return
     */
    @PutMapping
    @ResponseBody
    public Result updateCheckIn(@RequestBody CheckIn checkIn){
        Boolean flag = checkInService.updateCheckIn(checkIn);
        if(flag) {
            return Result.success();
        }else {
            return Result.error("修改信息失败");
        }
    }

    /**
     * 删除
     * @param checkIn
     * @return
     */
    @DeleteMapping
    @ResponseBody
    public Result deleteCheckIn(@RequestBody CheckIn checkIn){
        Boolean flag = checkInService.deleteCheckIn(checkIn);
        if(flag) {
            return Result.success();
        }else {
            return Result.error("删除失败");
        }
    }

    /**
     * 分页查询
     * @param requestParams
     * @return
     */
    @PostMapping("/selectByPage")
    @ResponseBody
    public Result page(@RequestBody Map<String, Integer> requestParams){
        Integer page = requestParams.getOrDefault("page", 1);
        Integer pageSize = requestParams.getOrDefault("pageSize", 10);
        PageBean pageBean = checkInService.page(page, pageSize);
        return Result.success(pageBean);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/ids")
    @ResponseBody
    public Result deleteByIds(@RequestBody List<String> ids) {
        Boolean flag = checkInService.deleteByIds(ids);
        if(flag){
            return Result.success();
        }else {
            return Result.error("批量删除失败");
        }
    }

    /**
     * 聚合搜索,网页搜索功能---搜索框提示词:老人姓名\老人电话\家属电话
     * @param JsonData
     * @return
     */
    @PostMapping("/searchByKeywords")
    @ResponseBody
    public Result searchByKeywords(@RequestBody Map<String,String> JsonData){
        String keywords = JsonData.get("keywords");
        List<CheckIn> checkIns = checkInService.searchByKeywords(keywords);
        if(!checkIns.isEmpty()){
            return Result.success(checkIns);
        }else {
            return Result.error("未查询到相关结果");
        }
    }

    /**
     * 数据分析可视化,获取入住日期及入住数量
     * @return
     */
    @GetMapping("/getCountByAcceptTime")
    @ResponseBody
    public Result getCountByAcceptTime(){
        List<Map<String, Object>> resultList = checkInService.getCountByAcceptTime();
        return Result.success(resultList);
    }

    /**
     * 成功办理入住
     * @param checkIn
     * @return
     */
    @PostMapping("/setCheckIn")
    @ResponseBody
    public Result setStatusByCheckInId(@RequestBody CheckIn checkIn){
        Boolean flag = checkInService.setStatusByCheckInId(checkIn);
        if(flag){
            //办理入住成功
            Elderly elderly = checkInService.autoAddElderly(checkIn);
            //添加老人
            elderlyMapper.add(elderly);
            return Result.success();
        }else {
            return Result.error("办理入住失败");
        }
    }

}
